Skip to content

Add Validation to Order Creation#1038

Open
Josiassejod1 wants to merge 6 commits intorubyforgood:mainfrom
Josiassejod1:dalvin/add-validation-to-execute-order
Open

Add Validation to Order Creation#1038
Josiassejod1 wants to merge 6 commits intorubyforgood:mainfrom
Josiassejod1:dalvin/add-validation-to-execute-order

Conversation

@Josiassejod1
Copy link
Collaborator

@Josiassejod1 Josiassejod1 commented Feb 19, 2026

Pull Request

Summary

There isn't current validation on ExecuteOrder when there are insufficient funds or shares, thus, this PR adds some tests and checks.

Related Issue

Resolves #1010

Changes

  • Add a valid_order check in the ExecuteOrder service that leverages similar logic that already exists in the order model

Screenshots (if applicable)

Checklist

  • [X ] Issue is assigned (commenting on the issue page is needed)
  • [X ] Issue link added to the PR's description
  • [X ] Branch created from main
  • [ X] Commits are small and descriptive
  • [X ] Ran linter and fixed issues
  • [ X] Ran tests and all tests pass
  • [X ] CI checks passing
  • Review requested from team members

Notes

@Josiassejod1 Josiassejod1 marked this pull request as ready for review February 19, 2026 05:39
Copy link
Collaborator

@pollygee pollygee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Josiassejod1 for your work here.
I don't think this current implementation is working to do the validations we need, so I've added some questions and comments. Check them out and let me know what you think If you have questions feel free to hit me up on slack!

Thanks again for your effort, we're on the right track and with some small updates this will be working just as needed.

order.sufficient_shares?
end

true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this is outside of the if block this function will always return true and that isn't what we want we'd want it to return the result of sufficient_funds or sufficient_shares.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good callout I need to call return

return true unless number_of_shares_valid_for_calculations?

current_shares = user.portfolio&.shares_owned(stock_id) || 0
shares <= current_shares
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we'll need to add an error if there aren't enough shares not just return false so we have an indication of why it didn't work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me!


current_balance_cents = (user.portfolio&.cash_balance || 0) * 100

fee_already_subtracted = user.orders.pending.exists? ? PortfolioTransaction::TRANSACTION_FEE_CENTS : 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the purpose of this function here here is not calculating "if" the fee was already calculated but rather what fee we need to add

maybe a better name could be fee_to_charge

order.save!
end

assert order.errors[:shares].first.include?("Insufficient funds")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as below, is this the error message we will get?

order.save!
end

assert order.errors[:base].first.include?("Cannot sell more shares than you own")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the error message we'd get I don't see it added as an error when we do the check.


total_needed = purchase_cost + transaction_fee

balance_without_reservations >= total_needed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add an error here to alert the user to why the transaction fails if it does fail.
Can you add something like:
errors.add(:shares, "Insufficient funds. You have #{formatted_balance} but need #{formatted_cost}")?

@Josiassejod1
Copy link
Collaborator Author

Thanks for the clarity !

@pollygee
Copy link
Collaborator

@AbuMareBear do you have any availability any time soon to take a look at these updates to the PR?

Copy link
Collaborator

@pollygee pollygee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this I think we're closer.
I still have some concerns here. We do already have validations on order for when an order is created.

This order execution happens in a job that runs at night so it isn't user facing. I think we might be able to reuse some of that work that has been done to validate enough stocks and cash to buy?

Feel free to ping me in slack if you want to talk through this a little more.

current_balance_cents = (user.portfolio&.cash_balance || 0) * 100

fee_to_charge = user.orders.pending.exists? ? PortfolioTransaction::TRANSACTION_FEE_CENTS : 0
balance_without_reservations = current_balance_cents + purchase_cost + fee_to_charge
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused with this line here, I'm not sure what this is calculating. How is the balance something we're getting from adding the purchase cose and fees to be charged?


return true if shares <= current_shares

formatted_shares = (current_shares % 1).zero? ? current_shares.to_i : current_shares
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this formatting because this is being run by a job so the user will not see an error message here at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add insufficient funds/shares validation to ExecuteOrder service

2 participants